package com.gxuwz.zhangjiahui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.Dep;
import com.gxuwz.zhangjiahui.entity.Student;
import com.gxuwz.zhangjiahui.entity.Team;
import com.gxuwz.zhangjiahui.service.IDepService;
import com.gxuwz.zhangjiahui.service.IStudentService;
import com.gxuwz.zhangjiahui.service.ITeamService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private ITeamService iTeamService;
    @Autowired
    private IStudentService iStudentService;


    /**
     * 查询全部信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/findTeam")
    public ModelAndView findTeam(ModelAndView modelAndView, Page page, Integer pageNumber,
                                    @Param("teamId") String teamId,
                                    @Param("keyTeam") Integer keyTeam,
                                    HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<Team> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("team_id");
        String[] keysWord = null;
        // 对User进行模糊查询!!!
        if(teamId != null && teamId != ""){
            wrapper.like("team_id", teamId);
            request.getSession().setAttribute("teamId", teamId);
        }else {
            request.getSession().setAttribute("teamId", "");
        }
        // 判断是否是加入/创建团队的学生
        if(keyTeam != null && keyTeam == 1){
            request.getSession().setAttribute("keyTeam", keyTeam);
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<Team> teamIPage = iTeamService.selectTeamPage(page, wrapper);
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)teamIPage.getPages()];
        for(int i=0; i< (int)teamIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", teamIPage.getTotal());
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+teamIPage.getTotal());
        System.out.println("总页数"+teamIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)teamIPage.getPages());
        List<Team> teamList = teamIPage.getRecords();
        System.out.println("teamList = "+teamList);
        modelAndView.addObject("teamList", teamList);

        modelAndView.setViewName("team/team_list");
        return modelAndView;
    }


    /**
     * 添加/修改 信息
     * @param team
     * @return
     */
    @PostMapping(value = "/addEditTeam")
    public ModelAndView addEditTeam(Team team, HttpServletRequest request, ModelAndView modelAndView) {
        Team team1 = iTeamService.findTeamById(team);
        System.out.println("team = " + team);
        System.out.println("team1 = " + team1);
        // 新增信息
        if(team1 == null){
            System.out.println("进入新增");
            // 人数默认为1
            team.setNum(1);
            try {
                iTeamService.addTeam(team);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改用户信息
        if(team1 != null){
            System.out.println("进入修改");
            try {
                iTeamService.updateTeamById(team);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/team/findTeam");
        return modelAndView;
    }



    /**
     * 删除信息
     * @param teamId
     * @return
     */
    @GetMapping(value = "/deleteTeam")
    public ModelAndView deleteTeam(HttpServletRequest request, ModelAndView modelAndView,
                                         @Param("teamId") String teamId) {
        Team team = new Team();
        team.setTeamId(teamId);
        try {
            iTeamService.deleteTeamById(team);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/team/findTeam");
        return modelAndView;
    }


    /**
     * 加入该团队
     * @param teamId
     * @return
     */
    @GetMapping(value = "/addNowTeam")
    public ModelAndView addNowTeam(HttpServletRequest request, ModelAndView modelAndView,
                                   @Param("teamId") String teamId) {
        // 查询该团队下的人数，人数需要<=5
        Team team = new Team();
        team.setTeamId(teamId);
        team = iTeamService.findTeamById(team);
        System.out.println("team = " + team);
        if(team != null){
            if(team.getNum() < 5){
                System.out.println("加入团队中------");
                Student student = new Student();
                String stuNo = (String) request.getSession().getAttribute("studentNo");
                student.setStuNo(stuNo);
                Student student1 = iStudentService.findStudentById(student);
                System.out.println("student = " + student);
                System.out.println("student1 = " + student1);
                student1.setTeamId(team.getTeamId());
                System.out.println("student1-2 = " + student1);
                iStudentService.updateStudentById(student1);

                // 修改团队信息
                team.setNum(team.getNum()+1);
                iTeamService.updateTeamById(team);

                // 纠正信息
                //该成员是学生，存在团队，但是不为队长
                request.getSession().setAttribute("teamStuNo", 2);

                request.getSession().setAttribute("result", "addTeamNow");
                request.getSession().setAttribute("nowResult", "now");
            }else {
                request.getSession().setAttribute("result", "addTeamNowFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/team/findTeam");
        return modelAndView;
    }
}
